/*
 * @Author: Dannycjh 2233879049@qq.com
 * @Date: 2023-04-19 12:57:28
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-19 13:04:22
 * @FilePath: \cc-game-development-template\assets\scripts\CameraFollow.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraFollow')
export class CameraFollow extends Component {
    @property(Node)
    target: Node | null = null;

    @property(Number)
    speed: number = 0;

    private _distance: Vec3 = new Vec3()

    protected onLoad(): void {
        Vec3.subtract(this._distance, this.node.worldPosition, this.target.worldPosition)
    }

    protected lateUpdate(dt: number): void {
        const targetPosition = new Vec3();
        const smoothPosition = new Vec3();
        Vec3.add(targetPosition, this.target.worldPosition, this._distance)
        Vec3.lerp(smoothPosition, this.node.worldPosition, targetPosition, dt * this.speed)
        this.node.worldPosition = smoothPosition
    }
    start() {
        
    }

    update(deltaTime: number) {

    }
}

